import type { InjectionKey } from 'vue';
import type { Store } from 'vuex';
import { LoginUser, USER_LOGIN, USER_LOGOUT } from '@/store/login/loginUser';

//声明类型
export interface GlobalStore {
  loginUser: LoginUser;
}

//定义store
export const globalStore = createStore<GlobalStore>({
  state() {
    return {
      //登录user
      loginUser: new LoginUser(),
    };
  },
  mutations: {
    [USER_LOGIN](state, user) {
      state.loginUser.userName = user.userName;
      state.loginUser.token = user.token;
      state.loginUser.role = user.role;
      state.loginUser.gcId = user.gcId;
    },
    [USER_LOGOUT](state) {
      state.loginUser = new LoginUser();
    },
  },
});

//定义injection key
export const key: InjectionKey<Store<GlobalStore>> = Symbol();

//定义组合式函数
export function useStoreGlobalStore() {
  return useStore(key);
}
